package com.lintf.bot.notice;

import com.lintf.bot.domain.BotNotice;
import com.lintf.bot.domain.dto.BotNoticeAddDTO;
import com.lintf.bot.domain.enums.NoticeTypeEnums;
import com.lintf.bot.domain.enums.SystemNoticeEnums;
import com.lintf.bot.rabbitmq.MessageProducer;
import com.lintf.bot.service.IBotNoticeService;
import com.lintf.bot.wechat.dto.NotificationsSendDTO;
import com.lintf.common.core.domain.entity.SysUser;
import com.lintf.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author lin tingfeng
 * @system Mac
 * @data 2024/3/21 16:14
 * @package_name com.lintf.system.notice
 * @project_name mobile-project
 * @desc
 */

@Component
public class NoticeUtils {

    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private IBotNoticeService botNoticeService;

    /**
     * 系统通知
     *
     * @param refId       推送消息关联数据id
     * @param noticeTitle 通知标题
     * @param enums       通知类型
     * @param title       微信推送标题
     * @param content     微信通知内容
     * @param createTime  微信通知时间
     * @param groupId     推送消息分组
     */
    public void systemPush(Integer refId, String noticeTitle, NoticeTypeEnums enums, String title, String content, Date createTime, Integer groupId) {
        BotNoticeAddDTO noticeAddDTO = new BotNoticeAddDTO(refId, noticeTitle, enums.getValue(), SystemNoticeEnums.System.getValue());
        noticeAddDTO.setGroupId(groupId);
        botNoticeService.add(noticeAddDTO);
        NotificationsSendDTO sendDTO = new NotificationsSendDTO(title, content, createTime, SecurityUtils.getLoginUser().getUser().getNickName(), SecurityUtils.getUserId(), groupId);
        messageProducer.sendNotification(sendDTO);

    }

    /**
     * 个人通知
     *
     * @param refId         推送消息关联数据id
     * @param noticeTitle   通知标题
     * @param enums         通知类型
     * @param title         微信推送标题
     * @param publishUserId 微信通知对象
     */
    public void personPush(Integer refId, String noticeTitle, NoticeTypeEnums enums, String title, Long publishUserId, Integer groupId) {
        Long userId = SecurityUtils.getUserId();
        if (Objects.equals(publishUserId, userId)) {
            return;
        }
        BotNoticeAddDTO noticeAddDTO = new BotNoticeAddDTO(refId, noticeTitle, enums.getValue(), SystemNoticeEnums.Person.getValue());
        noticeAddDTO.setToUserId(publishUserId);
        noticeAddDTO.setGroupId(groupId);
        botNoticeService.add(noticeAddDTO);
        NotificationsSendDTO sendDTO = new NotificationsSendDTO(title, publishUserId, userId, SecurityUtils.getLoginUser().getUser().getNickName());
        messageProducer.sendLikeNotification(sendDTO);
    }

    /**
     * 待办指定人员推送
     *
     * @param refId        推送消息关联数据id
     * @param noticeTitle  通知标题
     * @param enums        通知类型
     * @param title        微信推送标题
     * @param publishUsers 微信通知对象组
     */
    public void personListPush(Integer refId, String noticeTitle, NoticeTypeEnums enums, String title, List<SysUser> publishUsers, List<Map<String, Object>> params, Integer groupId) {
        List<BotNotice> list = new ArrayList<>();
        BotNotice notice;
        if (enums == NoticeTypeEnums.TO_DO_PROCESSING) {
            for (Map<String, Object> param : params) {
                Integer refIdInner = (Integer) param.get("refId");
                Long refUserId = (Long) param.get("refUserId");
                notice = new BotNotice(refIdInner, noticeTitle, enums.getValue(), SystemNoticeEnums.Person.getValue());
                notice.setToUserId(refUserId);
                notice.setGroupId(groupId);
//                notice.setCreateParams();
                list.add(notice);
            }
        } else {
            for (SysUser user : publishUsers) {
                notice = new BotNotice(refId, noticeTitle, enums.getValue(), SystemNoticeEnums.Person.getValue());
                notice.setToUserId(user.getUserId());
                notice.setGroupId(groupId);
//                notice.setCreateParams();
                list.add(notice);
            }
        }
        botNoticeService.saveBatch(list);
        NotificationsSendDTO sendNotificationDTO = new NotificationsSendDTO(title, SecurityUtils.getUserId(), SecurityUtils.getLoginUser().getUser().getNickName(), publishUsers);
        messageProducer.sendItemNotification(sendNotificationDTO);
    }
}
